DSCI 510
Fall 2020
Homework #2 (100 points)
DUE Tuesday, 9/27 at 11:59PM, submitted via the course website.
The first thing to note, is that this assignment is to be turned in via Python script files and text files, not Jupyter notebooks. That doesn’t mean you can’t do whatever development and debugging you’d like in a notebook, but your final submission deliverable is a zip file to be called [YOUR_FIRSTNAME]_ [YOUR_LASTNAME]_homework2.zip. For me, I would turn in a file called JEREMY_ ABRAMSON_homework2.zip.
In this zip file should be 6 files, named 1_firstname_lastname.py (i.e. 1_jeremy_abramson.py, 2_jeremy_abramson.py and so on)
You may lose points if you do not follow the submission procedure.
Question 1 (10 points)
Assuming you typed the following in your Python interpreter:
ten = 1
one = 10
zero = -1
What is the output of the following (assume there’s a print statement for each problem):
a). list(range(one))
b). list(range(zero, one))
c). list(range(zero, one))[zero]
d). list(range(zero, one))[list(range(zero, one))[one – 1]]
e). list(range(one, ten, zero))[ten – zero : one + zero ]
Question 2 (15 points)
Write a “coin receiver” simulation. The program should ask the user for what coins they want to add. The simulation accepts 10 cent, 25 cent, and 50 cent coins. All other coins are invalid, and should prompt the user to retry. A -1 entry ends the program. When the program ends, the simulation should print out the amount of money the user has input using the largest denomination possible. The available denominations are: all the coins mentioned above, 1, 2 and 5 dollar bills. Also print out the total value of the money inputted.
Examples:
>>> Enter a coin! 50
>>> Enter a coin! 50
>>> Enter a coin! 50
>>> Enter a coin! 50
>>> Enter a coin! -1
I can make change as:
1 $2 bill
For a total of $2 dollars
>>> Enter a coin! 10
>>> Enter a coin! 25
>>> Enter a coin! 50
>>> Enter a coin! 4
Please enter a valid coin value!
>>> Enter a coin! -1
I can make change as:
1 50 cent coin
1 25 cent coin
1 10 cent coin
For a total of 85 cents
>>> Enter a coin! 25
>>> Enter a coin! 25
I can make change as:
1 50 cent coin
For a total of 50 cents
Question 3 (10 points)
Consider a program that prompts the user for input, and adds that input to a list. When the user enters in ‘done’, the program prints out the list, and prompts the user to enter in an index. The user then enters an index, and the program prints out the value at that index and then terminates. The program should catch exceptions if an invalid index is entered, and prompt for re-entry.
Write pseudocode for this
Question 4 (25)
Write the python program for question 3.
Question 5 (10 points)
Consider a python function that creates a copy of an input list and converts all negative numbers in the input list to their absolute value in the returned output list. If an item in the input list is a non-numeric value, the function should not include it in the output list, unless it can be converted to a numeric value. For example:
Input1 = [‘DSCI-510’, -1, 0.1, 2, ‘US’, 3, ‘-3’]
>>> question2(input1)
[1, 0.1, 2, 3, 3].
Note that the last element in the input1 list is a string, but is still converted. Use exception handling to check to see if such a string conversion is possible, and if so, convert it to the appropriate numerical value. Remember: Naked exception handling is bad! Be sure to call your function with a number of examples in your program.
Write pseudocode for this function
Question 6 (30 points)
Write this function
***
NOTE: We have a corpus of common solutions to these problems (from google, stackoverflow, previous semesters, etc.). If your solution is too similar/the same as other solutions (i.e. if you cheat), you will get a zero for this assignment and be subject to potential sanction from SJA. Please don’t.